package com.woniuxy.closer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.closer.ResponseEntity;
import com.woniuxy.closer.entity.ContactsV;
import com.woniuxy.closer.mapper.ContactsVMapper;
import com.woniuxy.closer.service.ContactsVService;
import org.apache.commons.codec.binary.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 李玉桃
 * @since 2022-07-20
 */
@Service
public class ContactsVServiceImpl extends ServiceImpl<ContactsVMapper, ContactsV> implements ContactsVService {

    public ResponseEntity export(Map map) throws IllegalAccessException {
        QueryWrapper<ContactsV> contactsWrapper = new QueryWrapper<>();
        Iterator iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry next = (Map.Entry) iterator.next();
            if (!next.getValue().equals("")) {
//                map.remove(next.getKey());
                contactsWrapper.like((String) next.getKey(), next.getValue());
            }
        }
        List<ContactsV> list = list(contactsWrapper);
        //创建工作簿 （使用了多态特性）
        Workbook wb = new HSSFWorkbook();
        //创建一个sheet页
        Sheet sheet = wb.createSheet("sheet1");
        //创建行
        Row row = null;
        //创建单元格
        Cell cell = null;

        Class clazz = ContactsV.class;
        Field[] declaredFields = clazz.getDeclaredFields();
        row = sheet.createRow(0);
        for (int i = 1; i < declaredFields.length; i++) {
            cell = row.createCell(i - 1);
            cell.setCellValue(declaredFields[i].getName());
        }

        for (int i = 0; i < list.size(); i++) {

            row = sheet.createRow(i + 1);
            System.out.println(row);

            for (int j = 1; j < declaredFields.length; j++) {
                cell = row.createCell(j - 1);
                System.out.print(declaredFields[j].getName() + ":");
                declaredFields[j].setAccessible(true);
                System.out.println(declaredFields[j].get(list.get(i)));
                cell.setCellValue(declaredFields[j].get(list.get(i)) != null ? declaredFields[j].get(list.get(i)).toString() : "");
            }

        }

        try {
            String name = UUID.randomUUID().toString().replace("-", "");
            FileSystemView fsv = FileSystemView.getFileSystemView();
            File com = fsv.getHomeDirectory();
            String path = com.getPath();
            File file = new File(path + File.separator + name + ".xlsx");
            FileOutputStream out = new FileOutputStream(file);
            wb.write(out);
            out.close();

            //流的关闭
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", StringUtils.newStringIso8859_1(StringUtils.getBytesUtf8(file.getName())));
            return new ResponseEntity(200, "", headers);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}
